<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Welcome extends CI_Controller {

	public function __construct() {
		parent::__construct();
	}


	public function index() {
		$data['title'] = "Post លេង";
		$data['open_graph'] = '<meta property="og:site_name" content="Post Leng.com" />
		<meta property="og:title" content="Description of the image:" />
		<meta property="og:type" content="article" />
		<meta property="og:url" content="http://postleng.com" />
		<meta property="og:image" content="" />
		<meta property="og:locale" content="en_US" />
		<meta property="og:description" content="Post leng is free post for everyone. You can post what ever you wanted!" />';
		$data['tag'] = $this -> mod_tag -> select_tag_random();
		$data['post_record'] = $this -> mod_tag -> get_all_post_count();
		$this -> load -> view(F_MASTER . 'index', $data);
	}

		// First page , view all record
	public function load_post() {
			$items_per_group = 20;//sanitize post value
			$group_number = filter_var($_POST["group_no"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
			if (!is_numeric($group_number)) {
				header('HTTP/1.1 500 Invalid number!');
				exit();
			}
			$position = ($group_number * $items_per_group);
			$results= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1),$items_per_group.",".$position,NULL,"post_id DESC");

			echo '<br />';
			
			if ($results) {
				//output results from database
				echo '<ul class="thumbnails">';
				foreach ($results->result() as $row) {
					if($row -> count_click_views<=0){
						$views="ដង ";
					}else{
						$views="ដង ";
					}
					$str = $row -> description;
					$v_random = rand(1,2);// random class in css
					$col = "";
					if ($v_random == 1) {
						$col = "col-md-2";
					} else {
						$col = "col-md-3";
					} 
					////////// text /////////////////////////
					echo '<div class="' . $col . '" id="thumnail_over" style="margin-left: 4px; margin-right: 10px; padding-left: 0; padding-right: 0;">';
					if  ($row -> img_name == NULL && $row -> embed_code==NULL && $row -> embed_img ==NULL ) {
						$str = substr($str, 0,150)."...";
						echo '<li class="thumbnail" id="speak">';
						//echo '<aside class="pquote">';
						
						echo '<a href="#" data-toggle="modal" onclick="popUpText(this)" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="" row_tag_text="'."".'" data_count_click_text="' . $row -> count_click_views . '" data-target="#basicModalfortext" class="popup-modal" row_id_text="' . $row -> post_id . '" row_desc_text="' . $row -> description . '"><p style="color: #33536D;">' .'<q>&nbsp;'. $str.'&nbsp;</q>' . '</p></a>';
						
						//echo '</aside>';
						echo '<span class="caption simple-caption" style="bottom: -52px !important;">';

						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view_text'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						
						echo '<div >'.$row->embed_code.'</div>';
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';

					//// Upload image //////////////
					} else if($row -> img_name!=NULL && $row -> embed_code==NULL && $row -> embed_img ==NULL) {
						if (strlen($str) > 30 && strlen($str) < 101) {
							$str = substr($str, 0, 30) . '...';
						}
						echo '<li class="thumbnail" >';
						echo '<a href="#" class="tag_name_herf" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'." ".'" row_tag="'." ".'" data-toggle="modal" onclick="popUpDetail(this)" data_count_click="' . $row -> count_click_views . '" data-target="#basicModal" class="popup-modal" 
						row_id="' . $row -> post_id . '" tag_id="'.$row->tag_id.'" row_des="' . $row -> description . '" row_img="' . $row -> img_name . '""><img src="' . $row -> img_name . '" /></a>';
						echo '<p class="text_description">'.$str.'</p><br />';
						echo '<span class="caption simple-caption">';
						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span id="count_related" class="counting_view'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';;

					//// embed image from other url  ///////
					} else if($row -> embed_img!=NULL && $row -> embed_code==NULL && $row ->  img_name ==NULL) {
						if (strlen($str) > 30 && strlen($str) < 101) {
							$str = substr($str, 0, 30) . '...';
						}
						echo '<li class="thumbnail" >';
						echo '<p class="text_description">'.$str.'</p>';
						echo '<a href="#" class="tag_name_herf" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'."".'" row_tag="'."".'" data-toggle="modal" onclick="popUpDetail(this)" data_count_click="' . $row -> count_click_views . '" data-target="#basicModal" class="popup-modal" 
						row_id="' . $row -> post_id . '" tag_id="'.$row->tag_id.'" row_des="' . $row -> description . '" row_img="' . $row -> embed_img . '""><img src="' . $row -> embed_img . '" /></a>';
						
						echo '<span class="caption simple-caption">';
						
						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';

					/////////////////// video ////////////////////////////
					} else if($row -> img_name==NULL && $row -> embed_img ==NULL && $row -> embed_code!=NULL) {
						
						$ser = array();
						$ser[] = $row -> embed_code;
						?>
						<script type="text/javascript">
							get_video();
							function get_video()
							{
								$(".oembed").oembed(null, 
								{
									embedMethod: "append", 
									maxWidth: 424,
									maxHeight: 268,
									vimeo: { autoplay: true, maxWidth: 200, maxHeight: 200}			
								});
								var array_embed_code = <?php echo json_encode($ser,JSON_UNESCAPED_SLASHES); ?>; /// convert to array javscript and escap slashes
								$("#stexpand"+<?php echo $row->post_id; ?>).oembed(array_embed_code[0]); /// validate url by concerting to video 	
							}
						</script>
						<?php
						$description = substr($row -> description, 0,30).'...';
						echo '<div onclick="emebd_video_image(this)" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'."".'" row_tag_name_embed="'."".'" row_count_click_embed="'.$row -> count_click_views.'" row_post_id_embed="'.$row -> post_id.'" row_code_url="'.$row->embed_code.'">';
						echo '<li class="thumbnail" >';
						echo '<span id="stexpand'.$row->post_id.'">' ;

						echo '<p>'.$description.'</p>';
						echo '</span>';
						echo '<br />';
						echo '<span class="caption simple-caption">';

						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view_embed'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';
						echo '</div>';
					}
					echo '</div>';
				}
				//end of result set
			}
			echo '</ul>';
		}
			public function facebookcomments() {

			$id = $this->uri->segment(4);
			$query = $this->mod_globals->select_where(PREFIX_TABLE.'posts',array('post_id' => $id));

			foreach ($query->result() as $row) {
			$image_path = $row->img_name;
				$description = $row->description;


			}

			$data['title'] = $description." | Shared To Facebook";
			$data['open_graph'] = '<meta property="og:site_name" content="Post Leng.com" />
			<meta property="og:title" content="Description of the image:" />
			<meta property="og:type" content="article" />

			<meta property="og:url" content="'.base_url().'welcome/facebooklikebutton/postleng-freepost/'.$id.'/facebook-shared-pages" />
			<meta property="og:image" content="'.$image_path.'" />
			<meta property="og:locale" content="en_US" />
			<meta property="og:description" content="'.$description.'" />';
			$data['id'] = $id;
			$data['description'] = $description;
			$data['image_path'] = $image_path;
			$data['tag'] = $this -> mod_tag -> select_tag_random();
			$data['post_record'] = $this -> mod_tag -> get_all_post_count();
			$this -> load -> view(F_MASTER . 'index', $data);
		}
		public function facebooklikebutton() {

			//$this->load->model("mod_posts");
			//$this-> mod_posts -> fblikebtn();
			$id = $this->uri->segment(4);
			$query = $this->mod_globals->select_where(PREFIX_TABLE.'posts',array('post_id' => $id));

						foreach ($query->result() as $row) {

						if($row->img_name == "" && $row->embed_img == "" && $row->embed_code == ""){
							$image_path = "http://www.postleng.com/templates/front_end/images/postleng.png";
						}else if($row->img_name != ""){

							$image_path = $row->img_name;
						}else if($row->embed_img != ""){

							$image_path = $row->embed_img;
						}else if($row->embed_code != ""){

							$image_path = $row->embed_code;
						}
				

				$description = $row->description;


			}

			$data['title'] = $description." | Shared To Facebook";
			$data['open_graph'] = '<meta property="og:site_name" content="Post Leng.com" />
			<meta property="og:title" content="Description of the image:" />
			<meta property="og:type" content="article" />

			<meta property="og:url" content="'.base_url().'welcome/facebooklikebutton/postleng-freepost/'.$id.'/facebook-shared-pages" />
			<meta property="og:image" content="'.$image_path.'" />
			<meta property="og:locale" content="en_US" />
			<meta property="og:description" content="'.$description.'" />';
			$data['id'] = $id;
			$data['description'] = $description;
			$data['image_path'] = $image_path;
			$data['tag'] = $this -> mod_tag -> select_tag_random();
			$data['post_record'] = $this -> mod_tag -> get_all_post_count();
			$this -> load -> view(F_MASTER . 'index', $data);
		}
		public function page_not_found(){
			$data['title'] = "Post លេង";
			//$this->load->view(B_MASTER.'index',$data);
			$data['open_graph'] = '<meta property="og:site_name" content="Post Leng.com" />
			<meta property="og:title" content="Description of the image:" />
			<meta property="og:type" content="article" />
			<meta property="og:url" content="http://postleng.com" />
			<meta property="og:image" content="" />
			<meta property="og:locale" content="en_US" />
			<meta property="og:description" content="Post leng is free post for everyone. You can post what ever you wanted!" />';

			$data['error'] = "error_page";
			$data['tag'] = $this -> mod_tag -> select_tag_random();
			$this -> load -> view(F_MASTER . 'index', $data);
		}

		public function db_page_not_found(){
			$data['title'] = "Post លេង";
			//$this->load->view(B_MASTER.'index',$data);
			$data['open_graph'] = '<meta property="og:site_name" content="Post Leng.com" />
			<meta property="og:title" content="Description of the image:" />
			<meta property="og:type" content="article" />
			<meta property="og:url" content="http://postleng.com" />
			<meta property="og:image" content="" />
			<meta property="og:locale" content="en_US" />
			<meta property="og:description" content="Post leng is free post for everyone. You can post what ever you wanted!" />';

			$data['error'] = "error_page";
			$data['tag'] = $this -> mod_tag -> select_tag_random();
			$this -> load -> view(F_MASTER . 'index', $data);
		}
		public function update_count(){	
			$post_id=$this->input->post('post_id');
			$current_count=$this->mod_globals->select_string(PREFIX_TABLE."posts","count_click_views",array('post_id'=>$post_id));

			$this->mod_globals->update(PREFIX_TABLE."posts",array('count_click_views'=>$current_count+1),array('post_id'=>$post_id));

			echo $current_count+1;
		}

		public function releated_post(){
			$tag = $_GET['tagId'];
			$results=$this->mod_tag->get_related_post($tag);

			foreach ($results->result() as $row){
					echo '<li >';
					echo '<span onclick="change_ajax_value(this)" row_id="'.$row -> post_id.'" row_des="' . $row -> description . '" row_img="' . $row -> embed_img . '""><img src="' . $row -> embed_img . '" /></span>'; 
					echo '</ll>';
				}
		}
		public function get_multi_tags(){
			$postId = $_GET['post_id'];

			$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$postId));
				foreach ($querys->result() as $key) {
					echo '<a class="badge" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
				}
		}

		public function get_embed_url($insert){
			$text = str_replace('https://www.youtube.com/watch?v=','//www.youtube.com/embed/',$insert);
			return $text;
		}

		public function addedComment(){
			$username = $this->input->post('name');
			$email = $this->input->post('email');
			$comment = $this->input->post('comment_user');
			$this->mod_globals->insert(PREFIX_TABLE.'feedback', array('username' => $username,'email' => $email,'comment'=>$comment,'status'=>1,'created_date'=>null));

			redirect();
		}

	}

	/* End of file welcome.php */
	/* Location: ./application/controllers/welcome.php */
